{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "49a34681",
   "metadata": {},
   "source": [
    "- 手写数字的MNIST数据库：http://yann.lecun.com/exdb/mnist/\n",
    "- 简单数据格式：http://pjreddie.com/projects/mnist-in-csv/\n",
    "- 训练集http://www.pjreddie.com/media/files/mnist_train.csv\n",
    "- 测试集http://www.pjreddie.com/media/files/mnist_test.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "554edcd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入相关包\n",
    "import numpy\n",
    "import scipy.special\n",
    "\n",
    "# 定义神经网络\n",
    "class NeuralNetwork:\n",
    "    # 初始化神经网络\n",
    "    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):\n",
    "        # 设置输入层 隐层 输出层数量\n",
    "        self.inodes = inputnodes\n",
    "        self.hnodes = hiddennodes\n",
    "        self.onodes = outputnodes\n",
    "        \n",
    "        # 设置权重\n",
    "        self.wih = numpy.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes, self.inodes))\n",
    "        self.woh = numpy.random.normal(0.0,pow(self.hnodes, -0.5),(self.hnodes, self.onodes))\n",
    "        \n",
    "        # 设置学习率\n",
    "        self.lr = learningrate\n",
    "        \n",
    "        # 激活函数\n",
    "        self.activation_function = lambda x: scipy.special.expit(x)\n",
    "        \n",
    "        pass\n",
    "    \n",
    "    # 训练神经网络\n",
    "    def train(self, inputs_list, targets_list):\n",
    "        # 转化为二维数组\n",
    "        inputs = numpy.array(inputs_list, ndmin=2).T\n",
    "        targets_list = numpy.array(targets_list, ndmin=2).T\n",
    "        \n",
    "        # 隐藏层计算\n",
    "        hidden_inputs = numpy.dot(self.wih, inputs)\n",
    "        hidden_outputs = self.activation_function(hidden_inputs)\n",
    "        # 输出层计算\n",
    "        final_inputs = numpy.dot(self.woh, hidden_outputs)\n",
    "        final_outputs = self.activation_function(final_inputs)\n",
    "        \n",
    "        # 计算误差\n",
    "        output_errors = targets_list - final_outputs\n",
    "        \n",
    "        # 权重分隔误差\n",
    "        hidden_errors = numpy.dot(self.woh.T, output_errors)\n",
    "        \n",
    "        # 更新隐藏层到输出层权重\n",
    "        self.woh +=self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))\n",
    "        \n",
    "        # 更新输入层到隐藏层权重\n",
    "        self.wih +=self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))\n",
    "        pass\n",
    "    \n",
    "    # 查询结果\n",
    "    def query(self,inputs_list):\n",
    "        # 将输入转换为二维数组\n",
    "        inputs = numpy.array(inputs_list, ndmin=2).T\n",
    "        # 隐藏层计算\n",
    "        hidden_inputs = numpy.dot(self.wih, inputs)\n",
    "        hidden_outputs = self.activation_function(hidden_inputs)\n",
    "        # 输出层计算\n",
    "        final_inputs = numpy.dot(self.woh, hidden_outputs)\n",
    "        final_outputs = self.activation_function(final_inputs)\n",
    "        return final_outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a1019101",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置入参\n",
    "input_nodes = 3\n",
    "hidden_nodes = 3\n",
    "output_nodes = 3\n",
    "learning_rate = 0.3\n",
    "\n",
    "# 创建神经网络\n",
    "n = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c347ae51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.7047481 ],\n",
       "       [0.57324479],\n",
       "       [0.57540365]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 输出\n",
    "n.query([1.0, 0.5, -1.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "889e111d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "无",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
